package xinyu.customer.news;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.logos.common.source.network.entity.BaseNetEntity;
import com.logos.tool.log.LogUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import xinyu.customer.R;
import xinyu.customer.adapter.NewsNoteAdapter;
import xinyu.customer.base.AppBaseFragment;
import xinyu.customer.bean.NewsNoteBean;
import xinyu.customer.news.data.NewsTabMainContract;
import xinyu.customer.news.data.NewsTabMainRemoteDataSource;
import xinyu.customer.news.data.NewsTabNoteContract;
import xinyu.customer.news.data.NewsTabNotePresenter;
import xinyu.customer.news.data.NewsTabNoteRemoteDataSource;
import xinyu.customer.util.SpConstant;
import xinyu.customer.widget.CustomToastUtil;

/**
 * description ： TODO:类的作用
 * author : SIGHT-WXC
 * email : 391335693@qq.com
 * date : 2020-04-15 15:22
 */
public class NewsTabNoteFragment extends AppBaseFragment implements NewsTabNoteContract.View, OnRefreshListener, OnLoadMoreListener{

    private FrameLayout mFlSearch;
    private RelativeLayout mRlGroup;
    private SmartRefreshLayout mRefreshLayout;
    private RecyclerView mRecycleView;
    private NewsTabNoteContract.Presenter mPresenter;
    private NewsNoteAdapter adapter;
    private int pageNum =1;
    private boolean hasNextPage = true;
    private List<NewsNoteBean> allData = new ArrayList<>();
    private List<NewsNoteBean> pageData = new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = new NewsTabNotePresenter(this,new NewsTabNoteRemoteDataSource());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = View.inflate(getContext(), R.layout.fragment_news_tab_note, null);
        initView(view);
        initRefresh();
        getData();
        return view;
    }

    private void getData() {
        mPresenter.getNoteList(pageNum, SpConstant.getUserId());
    }

    private void initRefresh() {
        adapter = new NewsNoteAdapter(getContext());
        mRecycleView.setAdapter(adapter);
        mRecycleView.setLayoutManager(new LinearLayoutManager(getContext()));

        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setOnLoadMoreListener(this);
    }


    private void initView(View view) {
        mFlSearch = view.findViewById(R.id.fl_search);
        mRlGroup = view.findViewById(R.id.rl_group);
        mRefreshLayout = view.findViewById(R.id.smart_layout);
        mRecycleView = view.findViewById(R.id.rv_friend);
    }

    @Override
    public void setPresenter(NewsTabNoteContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(String message) {

    }

    @Override
    public void onNoteListSuccess(BaseNetEntity<List<NewsNoteBean>> listBaseNetEntity) {
        if (listBaseNetEntity == null) {
            return;
        }
        if (pageNum == 1) {
            allData.clear();
        }
        pageData.clear();

        LogUtil.debug("===",listBaseNetEntity.getResult().size()+"---");
        pageData.addAll(listBaseNetEntity.getResult());
        allData.addAll(pageData);
//        if (allData == null || allData.size() == 0) {
//            mCustomNoData.setVisibility(View.VISIBLE);
//            mRefreshLayout.setVisibility(View.GONE);
//        } else {
            adapter.resetItem(allData);
            adapter.notifyDataSetChanged();
//            mRefreshLayout.setVisibility(View.VISIBLE);
//            mCustomNoData.setVisibility(View.GONE);
//        }

        if (pageData == null || pageData.size() == 0) {
            hasNextPage = false;
        } else {
            hasNextPage = true;
        }
        mRefreshLayout.finishLoadMore();
        mRefreshLayout.finishRefresh();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        if (hasNextPage) {
            pageNum++;
            getData();
        } else {
            CustomToastUtil.showShort("暂无更多");
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadMore();
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNum = 1;
        getData();
    }
}
